import { MessagePlugin } from 'tdesign-vue-next'
import { ref } from 'vue'
let connect = null
const newMsg = ref('')
const getConnect = () => {
  if ('WebSocket' in window) {
    // websocket后面跟用户token
    connect = new WebSocket('ws://localhost:8000/websocket/' + localStorage.getItem('Ip'))
    connect.onmessage = (evt) => {
      newMsg.value = evt.data
    }
  } else {
    MessagePlugin.error('你的浏览器不支持websocket,请手动刷新获取最新广播')
  }
}

const onopen = () => {
  connect.onopen = () => {
    console.log('WebSocket连接成功，状态码：', connect.readyState)
  }
}

const onclose = () => {
  connect.onclose = () => {
    console.log('连接服务端失败')
  }
}
export {
  newMsg,
  onopen,
  onclose,
  getConnect
}